
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { Public } from 'src/public/public.decorator';
import { UploadService } from './upload.service';
import { CreateUploadDto } from './dto/create-upload.dto';
import { UpdateUploadDto } from './dto/update-upload.dto';
import * as OSS from 'ali-oss';
import * as fs from 'fs';
// import * as sharp from 'sharp';
import path, { join } from 'path';
import {
  FileInterceptor,
  FilesInterceptor,
} from '@nestjs/platform-express/multer';

@Controller('upload')
export class UploadController {
  private ossClient: OSS;
  constructor(private readonly uploadService: UploadService,) {
    this.ossClient = this.ossClient = new OSS({
      region: process.env.REGION,
      accessKeyId: process.env.ACCESS_KEY_ID,
      accessKeySecret: process.env.ACCESS_KEY_SECRET,
      bucket: process.env.BUCKET,
    });
  }
  @Public()
  @Post('album')
  @UseInterceptors(FileInterceptor('file'))
  async upload(@UploadedFile() file: any) {
    const { originalname, filename, path } = file;
    console.log(path, 'album');

    // 获取上传目录路径
    try {
      // if (file.mimetype == 'image/gif') {
      //   const compressedImageBuffer = await sharp(file.path, {
      //     animated: true,
      //     limitInputPixels: false
      //   }).gif({ colours: 10 }).toBuffer();
      //   const result = await this.ossClient.put('images/' + file.originalname, compressedImageBuffer, {
      //     headers: {
      //       'x-oss-object-acl': 'public-read' // 设置对象 ACL 为公共读
      //     },
      //     'Content-Type': file.mimetype, // 设置图片的 Content-Type
      //   });
      //   return result.url;
      // } else 
      console.log(path,'path');
      
      let url = 'http://101.201.224.91:3011/images/'+ filename
      return url
      if (file.mimetype.indexOf('mp4') > -1) {
        const result = await this.ossClient.put('images/' + file.originalname, path, {
          headers: {
            'x-oss-object-acl': 'public-read' // 设置对象 ACL 为公共读
          },
          'Content-Type': 'image/jpg', // 设置图片的 Content-Type
        });
        return result.url;
      } else {

        const result = await this.ossClient.put('images/' + file.originalname, file.path, {
          headers: {
            'x-oss-object-acl': 'public-read' // 设置对象 ACL 为公共读
          },
          'Content-Type': file.mimetype, // 设置图片的 Content-Type
        });
        //压缩
        // const compressedImageBuffer = await sharp(path)
        //   .jpeg({ quality: 10 }) // Adjust quality as needed
        //   .toBuffer();
        // const result = await this.ossClient.put('images/' + file.originalname, compressedImageBuffer, {
        //   headers: {
        //     'x-oss-object-acl': 'public-read' // 设置对象 ACL 为公共读
        //   },
        //   'Content-Type': file.mimetype, // 设置图片的 Content-Type
        // });
        return result.url;
      }
    } catch (error) {
      throw new HttpException('上传失败', HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Get()
  findAll() {
    return this.uploadService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.uploadService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUploadDto: UpdateUploadDto) {
    return this.uploadService.update(+id, updateUploadDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.uploadService.remove(+id);
  }
}
